<?php
class BrandAttentionModel extends BaseModel
{
    /**
     * 添加一个用户关注的品牌
     * @return int
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function addAtt($uid, $bid, $mid = 0, $fid = 0)
    {
        $data = array();
        $data['uid'] = $uid;
        $data['bid'] = $bid;
        if ($mid) {
            $data['mid'] = $mid;
        }
        if ($fid) {
            $data['fid'] = $fid;
        }
        $data['create_time'] = time();
        return $this->add($data);
    }

    /**
     * 删除一个用户关注的品牌
     * @return int
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function deleteAtt($uid, $bid)
    {
        $map = array();
        $map['uid'] = $uid;
        $map['bid'] = $bid;
        return $this->where($map)->delete();
    }

    /**
     * 根据用户和品牌查询详细
     * @param int $uid 用户
     * @param int $bid 品牌
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function findAtt($uid, $bid)
    {
        $map = array();
        $map['uid'] = $uid;
        $map['bid'] = $bid;
        return $this->where($map)->find();
    }

    /**
     * 获取品牌关注数量
     * @param int $bid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAttCount($bid)
    {
        $map = array();
        $map['bid'] = $bid;
       	
        return $this->where($map)->count();
    }

    public function getUserAttCount($uid)
    {
        $map = array();
        $map['uid'] = $uid;
        return $this->where($map)->count();
    }

    public function checkUserIsAttrBrand($uid, &$brand)
    {
        $att = $this->findAtt($uid, $brand['id']);
        if ($att) {
            $brand['is_att'] = 1;
        } else {
            $brand['is_att'] = 0;
        }
    }

    /**
     * 判断品牌列表中是否已经被关注
     * @param int $uid
     * @param array $mall
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function checkUserIsAttrBrands($uid, &$brands)
    {
        if (is_array($brands)) {
            foreach ($brands as $key => $brand) {
                $att = $this->findAtt($uid, $brand['id']);
                if ($att) {
                    $brands[$key]['is_att'] = 1;
                } else {
                    $brands[$key]['is_att'] = 0;
                }
            }
        }
    }

    /**
     * 根据用户获取关注品牌的活动
     * @param $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandsNew($uid)
    {
        //1.获取关注的10个品牌
        //2.获取品牌的柜台资讯
        $map = array();
        $map['uid'] = $uid;
        $brands = $this->field('bid')->group('bid')->limit('0,10')->where($map)->select();
        $data = array();
        if (!empty($brands)) {
            $BrandModel = D('Brand');
            $BrandNewsModel = D('BrandNews');
            foreach ($brands as $brand) {
                $bid = $brand['bid'];
                unset($map);
                $map['bid'] = $bid;
                $map['end_time'] = array('egt', $this->getThisDayTime());
                $news = $BrandNewsModel->field('id,title,image,mid,fid,bid,keyword,start_time,end_time')->where($map)->group('mid,fid,bid')->select();
                if (!empty($news)) {
                    foreach ($news as $new) {
                        unset($mfb);
                        $mfb = $this->getAdMFB($new['mid'], $new['fid'], $new['bid']);
                        $new = array_merge($new, $mfb);
                        $new['ad_url'] = strtolower(U('/' . $mfb['mall']['initial'] . '/' . $mfb['floor']['sain'] . '/' . $mfb['brand']['ename']));
                        $data[$bid]['news'][] = $new;
                        //如果没有图片信息字获取
                        if (empty($data[$bid]['brand'])) {
                            $data[$bid]['brand'] = $mfb['brand'];
                            $data[$bid]['brand']['image_url'] = $BrandModel->getBrandImage($mfb['brand']['image']);
                        }
                    }
                }
            }
        }
        return $data;
    }

    /**
     * 根据用户获取关注品牌的特卖活动
     * @param $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandsDis($uid)
    {
        return null;
    }

    /**
     * 获取用户关注品牌首字母
     * @param $uid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getInitials($uid)
    {
        $map = array();
        $map['m_brand_attention.uid '] = $uid;
        return $this->field('m_brand.initials')
            ->where($map)
            ->join('INNER JOIN m_brand ON m_brand_attention.bid = m_brand.id')
            ->order('m_brand.initials')
            ->group('m_brand.initials')
            ->select();
    }

    /**
     * 获取对应品牌数据
     * @param $uid
     * @param $i
     * @return mixed
     */
    public function getInitialsBrands($uid, $i)
    {
        $map = array();
        $map['m_brand_attention.uid '] = $uid;
        $map['m_brand.initials '] = $i;
        $brands = $this->field('m_brand.id, m_brand.image, m_brand.name, m_brand.ename')
            ->where($map)
            ->join('INNER JOIN m_brand ON m_brand_attention.bid = m_brand.id')
            ->select();
        if ($brands) {
            $BrandModel = D('Brand');
            foreach ($brands as $key => $brand) {
                $brands[$key]['image_url'] = $BrandModel->getBrandImage($brand['image']);
            }
        }
        return $brands;
    }
}

?>